package com.ekuaitu.kuaitustaff.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class TimeUtils {

    // 获取当前日期
    public static String getCurrentYearMonthDay() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(calendar.getTime());
    }


    public static int daysBetween(String smdate, String bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(sdf.parse(smdate));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            return Integer.parseInt(String.valueOf(between_days));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //格式化秒
    public static String formatSecond(String second) {
        String dateString = "0秒";
        if (!second.equals("")) {
            Double s = Double.parseDouble(second);
            String format;
            Object[] array;
            Integer days = (int) (s / (24 * 60 * 60));
            Integer hours = (int) (s / (60 * 60) - days * 24);
            Integer minutes = (int) (s / 60 - days * 24 * 60 - hours * 60);
            Integer seconds = (int) (s - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60);
            if (days > 0) {
                format = "%1$,d天%2$,d小时%3$,d分%4$,d秒";
                array = new Object[]{days, hours, minutes, seconds};
            } else if (hours > 0) {
                format = "%1$,d小时%2$,d分%3$,d秒";
                array = new Object[]{hours, minutes, seconds};
            } else if (minutes > 0) {
                format = "%1$,d分%2$,d秒";
                array = new Object[]{minutes, seconds};
            } else {
                format = "%1$,d秒";
                array = new Object[]{seconds};
            }
            dateString = String.format(format, array);
        }
        return dateString;
    }

    //格式化分
    public static String formatMin(String second) {
        String dateString = "0分钟";
        if (!second.equals("")) {
            Double s = Double.parseDouble(second);
            if (s < 60) {
                return "0分钟";
            }
            String format;
            Object[] array;
            Integer days = (int) (s / (24 * 60 * 60));
            Integer hours = (int) (s / (60 * 60) - days * 24);
            Integer minutes = (int) (s / 60 - days * 24 * 60 - hours * 60);
            Integer seconds = (int) (s - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60);
            if (days > 0) {
                format = "%1$,d天%2$,d小时%3$,d分钟";
                array = new Object[]{days, hours, minutes, seconds};
            } else if (hours > 0) {
                format = "%1$,d小时%2$,d分钟";
                array = new Object[]{hours, minutes, seconds};
            } else {
                format = "%1$,d分钟";
                array = new Object[]{minutes, seconds};
            }
            dateString = String.format(format, array);
        }
        return dateString;
    }

    //格式化分加一秒
    public static String formatMinPlusOne(String second) {
        String dateString = "";
        if (!second.equals("")) {
            Double s = Double.parseDouble(second);
            if (s != 0) {
                s += 60;
            }
            String format;
            Object[] array;
            Integer days = (int) (s / (24 * 60 * 60));
            Integer hours = (int) (s / (60 * 60) - days * 24);
            Integer minutes = (int) (s / 60 - days * 24 * 60 - hours * 60);

            if (days > 0) {
                format = "%1$,d天%2$,d小时%3$,d分钟";
                array = new Object[]{days, hours, minutes};
            } else if (hours > 0) {
                format = "%1$,d小时%2$,d分钟";
                array = new Object[]{hours, minutes};
            } else {
                format = "%1$,d分钟";
                array = new Object[]{minutes};
            }
            dateString = String.format(format, array);
        }
        return dateString;
    }


    /**
     * @param second
     * @param formatType 显示格式
     *                   1      00:00:00
     *                   2      00:00
     *                   3     00
     * @return
     */
    //格式化秒
    public static String formatSecondCount(String second, int formatType) {
        String dateString = "00:00:00";
        if (!second.equals("")) {
            Double s = Double.parseDouble(second);
            String format;
            Object[] array;
        /*    Integer days = (int) (s / (24 * 60 * 60));*/
            Integer hours = (int) (s / (60 * 60));
            Integer minutes = (int) (s / 60 - hours * 60);
            Integer seconds = (int) (s - hours * 60 * 60 - minutes * 60);
            if (formatType == 1) {
                format = "%1$,02d:%2$,02d:%3$,02d";
                array = new Object[]{hours, minutes, seconds};
            } else if (formatType == 2) {
                format = "%1$,02d:%2$,02d";
                array = new Object[]{minutes, seconds};
            } else {
                format = "%1$,02d";
                array = new Object[]{seconds};
            }
            dateString = String.format(format, array);
        }
        return dateString;
    }


    //格式化时间
    public static String getFormatDate(String timestampString, String formatString) {
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        long unixLong = 0;
        String date = "";
        try {
            switch (timestampString.length()) {
                case 13:
                    unixLong = Long.parseLong(timestampString);
                    break;
                case 10:
                    unixLong = Long.parseLong(timestampString) * 1000;
                    break;
            }
            date = format.format(unixLong);
        } catch (Exception ex) {
            return "";
        }
        return date;
    }

    //转为时间戳
    public static String getTimestampDate(String dateString, String formatString) {
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        try {
            return String.valueOf(format.parse(dateString).getTime());
        } catch (Exception e) {
            return "";
        }
    }
}
